module.exports = {
  // 根据域名查询IP地址
  getIPFromChain: function (domain, dnsContract) {
    return new Promise(function (resolve, reject) {
      // 从合约查询
      dnsContract.methods
        .getIPbyDomain(domain)
        .call()
        .then((result) => {
          resolve(result);
        })
        .catch((err) => {
          reject(err);
        });
    });
  },
  // 向区块链添加域名记录
  addRecordToChain: function (dnsContract, account, password, domain, IP) {
    // 向合约写入
    return new Promise(function (resolve, reject) {
      dnsContract.methods
        .addToMap(domain, IP)
        .send({ from: account })
        .then(function (receipt) {
          resolve(receipt);
        })
        .catch((err) => {
          reject(err);
        });
    });
  },
  unlock: function (web3, account, password) {
    return new Promise((resolve, reject) => {
      web3.eth.personal
        .unlockAccount(account, password, 600)
        // 解锁成功
        .then(resolve("UNLOCK_SUCCESS"))
        .catch(reject("UNLOCK_FAILED"));
    });
  },
};
